#include "Game.h"
#include<QPainter>
#include<QTimer>
#include<QDebug>
#include<QKeyEvent>
#include"PlayerPlane.h"
#include"Enemyplane.h"
#include<QPixmap>
#include<qrandom.h>
#include"global.h"
#include"Manager.h"
#include"Bullett.h"
PlayerPlane *player;
int score=0;
int j=5;
Game::Game(QWidget *parent)
    : QWidget{parent}
{
    QTimer *timer=new QTimer;
    timer->callOnTimeout(this,[=](){
        updateGame();
        update();
    });
    timer->start(1000/70);

}
void Game::run(){
    this->show();
}
void Game::initGame(int w,int h,const QString &title){
    setFixedSize(w,h);
    setWindowTitle(title);
    player=Mgr->addEntity(new PlayerPlane(100,100,190,760,":/new/prefix1/game/3.png"));
    player->setType(EntityType::ET_Player);

}
void Game::cleanGame(){

}
void Game::updateGame(){
    Mgr->refersh();
    Mgr->update();
    if(player->position.x()<0){
        player->position.setX(0);
    }
    if(player->position.y()<0){
        player->position.setY(0);
    }
    if(player->position.x()>380){
        player->position.setX(380);
    }
    if(player->position.y()>760){
        player->position.setY(760);
    }
    static int i=0;
    if(i%35==0){
    player->fire();
    }
    QStringList es={":/new/prefix1/game/4.jpg",":/new/prefix1/game/5.png"};
    if(i%35==0){
        auto e=Mgr->addEntity(new EnemyPlane(50,50,0,0,es[QRandomGenerator::global()->bounded(2)]));
        e->setpos(QRandomGenerator::global()->bounded(480-e->width()),-e->height());
        e->velocity.setY(1);
        e->setType(EntityType::ET_Enemy);
    }
    i++;
    for(auto e:Mgr->getEntityByType<EnemyPlane>(EntityType::ET_Enemy)){
        for(auto b:Mgr->getEntityByType<Bullett>(EntityType::ET_Bullett)){
            bool s=false;
            if(e->collider.intersects(b->collider)){
                e->destroy();
                b->destroy();
                score+=20;
                s=true;
                break;
            }
            if(!s&&player->collider.intersects(e->collider)){
                e->destroy();
                close();
            }
        }
    }

}
void Game::drawGame(QPainter *painter){
    QPixmap pixmap1(":/new/prefix1/game/7.png");
    QPixmap map=pixmap1.scaled(480,860);
    painter->drawPixmap(0,0,map);
    QString s1("积分：");
    s1+=QString::number(score);
    painter->drawText(400,50,s1);
    Mgr->draw(painter);

}
void Game::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    drawGame(&painter);
}
void Game::closeEvent(QCloseEvent *ev){
    cleanGame();
}
void Game::keyPressEvent(QKeyEvent *ev){
    switch(ev->key()){
    case Qt::Key_Up:
        player->velocity.setY(-1);
        break;
    case Qt::Key_Down:
        player->velocity.setY(1);
        break;
    case Qt::Key_Left:
        player->velocity.setX(-1);
        break;
    case Qt::Key_Right:
        player->velocity.setX(1);
        break;
    }
}
void Game::keyReleaseEvent(QKeyEvent *ev){
    switch(ev->key()){
    case Qt::Key_Up:
        player->velocity.setY(0);
        break;
    case Qt::Key_Down:
        player->velocity.setY(0);
        break;
    case Qt::Key_Left:
        player->velocity.setX(0);
        break;
    case Qt::Key_Right:
        player->velocity.setX(0);
        break;
    }
}
